// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const TcbRouter = require('tcb-router')

const db = cloud.database()
const wxContext = cloud.getWXContext()
const shouchangCollection = db.collection('shouchang')
const MAX_LIMIT = 100

// 云函数入口函数
exports.main = async(event, context) => {
  const app = new TcbRouter({
    event
  })

// 图片
  app.router('getListByOpenid', async(ctx, next) => {
    ctx.body = await shouchangCollection.where({
        _openid: wxContext.OPENID,
        ip:111
      }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
  })
  // 帖子
  app.router('getListByOpenid2', async(ctx, next) => {
    ctx.body = await shouchangCollection.where({
        _openid: wxContext.OPENID,
        ip:222
      }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
  })
  // 视频
  app.router('getListByOpenid3', async(ctx, next) => {
    ctx.body = await shouchangCollection.where({
        _openid: wxContext.OPENID,
        ip:333
      }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
  })


  return app.serve()
}